<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
    <button>发送请求</button>
    <button>取消请求</button>
    <script>
        let oBtn1 = document.querySelectorAll('button')[0]
        let oBtn2 = document.querySelectorAll('button')[1]
        let cancel = null;
        oBtn1.onclick = function(){
            axios({
                method:'get',
                url:'http://localhost:8080/timeout',
                cancelToken: new axios.CancelToken(c=>{
                    cancel = c;
                })
            }).then(data=>{
                console.log(data.data);
            })
        }

        oBtn2.onclick = function(){
            cancel();
        }
    </script>
</body>
</html>